﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.member.order
{
    public partial class sell_19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind1();
            }
        }
        private void bind1()
        {
            taobaotrade t = taobaotrade.Find(" seller_nick = '"
                + AliApi.appUserId + "'");
            WebPager1.DataSource = t.MyTable;
            WebPager1.DataBind();
        }

        protected void WebPager1_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            WebPager1.CurrentPageIndex = e.NewPageIndex;
            bind1();
        }

        protected void SearchOrderButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string filter = " seller_nick='"+AliApi .appUserId +"' ";
                if (ItemNameTextBox.Text.Trim() != string.Empty)
                {
                    filter += " and taobaoname like '%"+ItemNameTextBox.Text.Trim ().Replace (" ","%")+"%' ";
                }
                if (BuyerNickTextBox.Text.Trim() != string.Empty)
                {
                    filter += " and buyer_nick like '" + BuyerNickTextBox.Text.Trim() + "' ";
                }
                if (this.PostServiceDropDownList.SelectedValue != string.Empty)
                {
                    filter += " and cbtrantype='" + PostServiceDropDownList.SelectedValue + "' ";
                }
                if (OrderStatusDropDownList.SelectedValue != string.Empty)
                {
                    if (OrderStatusDropDownList.SelectedValue == "未结订单")
                        filter += " and (taobaostatus!='交易完成' and taobaostatus!='交易关闭') "; 
                    else
                        filter += " and taobaostatus='" + OrderStatusDropDownList.SelectedValue + "'";
                }
                if (TradeRateDropDownList.SelectedValue != string.Empty)
                {
                    filter += " and ";
                    switch (TradeRateDropDownList.SelectedValue)
                    {
                        case "需我评价":
                            {
                                filter += " (taobaoseller_rate !='true') ";
                                break;
                            }
                        case "我已评价":
                            {
                                filter += " (taobaoseller_rate ='true') ";
                                break;
                            }
                        case "对方未评":
                            {
                                filter += " (taobaobuyer_rate !='true') ";
                                break;
                            }
                        case "对方已评":
                            {
                                filter += " (taobaobuyer_rate ='true') ";
                                break;
                            }
                        case "双方已评":
                            {
                                filter += " (taobaobuyer_rate ='true' and taobaoseller_rate ='true') ";
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }

                if (OrderFromTextBox.Text.Trim() != string.Empty)
                {
                    DateTime orderDate = DateTime.MinValue ;
                    try
                    {
                        orderDate = Convert.ToDateTime(OrderFromTextBox.Text.Trim());
                    }
                    catch { }
                    if (orderDate != DateTime.MinValue)
                    {
                        filter += " and cast (taobaocreated as DateTime )>'" + orderDate.ToString() + "' ";
                    }
                }
                if (OrderToTextBox.Text.Trim() != string.Empty)
                {
                    DateTime orderDate = DateTime.MaxValue  ;
                    try
                    {
                        orderDate = Convert.ToDateTime(OrderToTextBox.Text.Trim());
                    }
                    catch { }
                    if (orderDate != DateTime.MaxValue)
                    {
                        filter += " and cast (taobaocreated as DateTime )<'" + orderDate.ToString() + "' ";
                    }
                }

                taobaotrade trade = taobaotrade.Find(filter);
                WebPager1.DataSource = trade.MyTable;
                WebPager1.DataBind();
            }
        }

        /// <summary>
        /// 获取评价状态
        /// </summary>
        /// <param name="seller_rate"></param>
        /// <param name="buyer_rate"></param>
        /// <returns></returns>
        protected string GetRateStr(string seller_rate, string buyer_rate)
        {
            string result = string.Empty;
            if (seller_rate == "true")
            {
                if (buyer_rate == "true")
                {
                    result = TRADE_RATE_STATUS.Both_Rate;
                }
                else
                {
                    result = TRADE_RATE_STATUS.Seller_Rate;
                }
            }
            else
            {
                if (buyer_rate == "true")
                {
                    result = TRADE_RATE_STATUS.Buyer_Rate;
                }
                else
                {
                    result = TRADE_RATE_STATUS.None_Rate;
                }
            }
            return result;
        }
    }
}
